package com.jf.springmvc.exception;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-11-05
 * Time:17:19
 */
@RequestMapping("/exception")
@Controller
public class LocalExceptionHandler {
    /*
    异常处理
    优先级: 局部异常处理 > 全局异常处理 > 统一指定异常处理 > 统一Exception异常处理 > 默认异常处理(Tomcat处理)
     */
    @ExceptionHandler({ArithmeticException.class})
    public String exceptionHandler(Exception ex, Model model) {
        model.addAttribute("message", ex.getMessage());
        System.out.println(ex.getMessage());
        return "exception";
    }
    @GetMapping("/test1")
    public String test1() {
        int i = 1 / 0;
        return "success";
    }
    @GetMapping("/test2")
    public String test2() {
        int[] arr = { 1, 2, 3 };
        System.out.println(arr[10]);
        return "success";
    }
    @GetMapping("/test3")
    public String test3() {
        throw new AgeException();
    }
    @GetMapping("/test4")
    public String test4() {
        String s =  "hello";
        System.out.println(s.charAt(100));
        return "success";
    }
    @GetMapping("/test5")
    public String test5() {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream("/aa.txt")));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        return "success";
    }
}
